14. Exercise: Error Handling with RecyclerView
L8 25 Error Handling With RecyclerView SC
Erratum
At timestamp 00:35 in the video above, inOverviewViewModel.ktfile, the definition of thegetMarsRealEstateProperties()function has been updated due to deprecated Retrofit 2 Call callbacks. Consider the new definition of thegetMarsRealEstateProperties()function as mentioned in the instructions below, or alternatively, you can check out the relevant solution branch of the Git repo.
Now it's your turn!
In this exercise you'll add error handling and status messages to make your app more robust. If you want to start at this step, you can download the code from: Step.06-Exercise-Error-Handling-with-RecyclerView. You will find plenty of //TODO comments in the exercise code to help you out.
In
OverviewViewModel, create aMarsApiStatusenumwithLOADING, ERROR, DONEstates.Note: We created it outside of the class, but it can go either place.
enum class MarsApiStatus { LOADING, ERROR, DONE }
- Change
_statustype fromStringtoMarsApiStatus.
private val _status = MutableLiveData<MarsApiStatus>()
val status: LiveData<MarsApiStatus>
get() = _status
- In
getMarsRealEstateProperties(), using theenumsdefined above, set_statusvalue toLOADING,DONE, orERROR. In the error case, clear the propertiesLiveDataby setting it to a new emptyArrayList:
private fun getMarsRealEstateProperties() {
viewModelScope.launch {
_status.value = MarsApiStatus.LOADING
try {
_properties.value = MarsApi.retrofitService.getProperties(filter.value)
_status.value = MarsApiStatus.DONE
} catch (e: Exception) {
_status.value = MarsApiStatus.ERROR
_properties.value = ArrayList()
}
}
}
In
BindingAdapters.kt, add a binding adapter to showMarsApiStatusin theImageView, and set the view's visibility depending on the status value:@BindingAdapter("marsApiStatus") fun bindStatus(statusImageView: ImageView, status: MarsApiStatus?) { when (status) { MarsApiStatus.LOADING -> { statusImageView.visibility = View.VISIBLE statusImageView.setImageResource(R.drawable.loading_animation) } MarsApiStatus.ERROR -> { statusImageView.visibility = View.VISIBLE statusImageView.setImageResource(R.drawable.ic_connection_error) } MarsApiStatus.DONE -> { statusImageView.visibility = View.GONE } } }In
fragment_overview, add astatus_imageImageViewto theConstraintLayout.Set its width and height to
wrap_content, and assign it the same layout constraints as theRecyclerView.Also include an attribute to bind the
marsApiStatusbinding adapter toviewModel.status:<ImageView android:id="@+id/status_image" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:marsApiStatus="@{viewModel.status}" />
If you get stuck, go back and watch the video again. Once you’re done, you can check your solution against the solution we’ve provided here: Step.06-Solution-Error-Handling-with-RecyclerView, or using this git diff.
Task Description:
Complete the tasks below to add error handling to your network calls.
Task Feedback:
Great job!